package cntv.player.core;

import cntv.player.core.util.LogUtil;
import com.cntv.cbox.player.core.CBoxP2PCore;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class P2PBufferTask implements Runnable {
	private Handler handler;
	private String bufferStr;
	private CBoxP2PCore mCBoxP2PCore;
	
	private int iDelayTime = 300;
	
	public P2PBufferTask(String bufferStr,Handler handler){
		this.bufferStr = bufferStr;
		this.handler = handler;
		this.mCBoxP2PCore = CBoxP2PCore.getInstance();
	}
	
	public void run(){
		int bufferState = 0;
		handler.removeMessages(CBoxStaticParam.P2P_BUFFER);
		handler.removeMessages(CBoxStaticParam.P2P_BUFFER_SUCCESS);
		handler.removeMessages(CBoxStaticParam.P2P_BUFFER_FAIL);
		
		try{
			bufferState = Integer.parseInt(mCBoxP2PCore.InstanceGetP2PState(bufferStr));
		}catch(Exception e){
			e.printStackTrace();
			sendError(0);
		}
		if(bufferState == 200){
			handler.sendEmptyMessage(CBoxStaticParam.P2P_BUFFER_SUCCESS);
		}else{
			if(bufferState == 404 || bufferState == 0){
				
				/***2013-12-20 新增 用于404超时***/
				Message msg = new Message();
				msg.what = CBoxStaticParam.P2P_BUFFER;
				msg.obj = bufferState;
				handler.sendMessageDelayed(msg,iDelayTime);
				
			}else{
				sendError(bufferState);
			}
		}
	}
	
	public void sendError(int errorNum){
		String errorStr = "";
		
		LogUtil.e("zl", "P2PBufferTask sendError() errorNum = " + errorNum);
		switch(errorNum){
		case 501:
			  errorStr = "您所在的地区处于限制播放区域！";
			  break;
		  case 502:
			  errorStr = "您所选择的节目信号源中断！\r\n请重新选择。";
			  break;
		  case 503:
			  errorStr = "播放数据正在准备。\r\n请稍候选择！";
			  break;
		  case 504:
			  errorStr = "因版权原因。\r\n本时段节目暂停播放！";
			  break;
		  case 506:
			  errorStr = "内核版本过低。\r\n请升级后观看！";
			  break;
	      default:
	    	  errorNum = 9999;
	    	  errorStr = "系统错误,请重新运行!";
	    	  break;
		}
		Message msg = new Message();
		msg.what = CBoxStaticParam.P2P_BUFFER_FAIL;
		Bundle errInfo = new Bundle();
		errInfo.putInt("errNum", errorNum);
		errInfo.putString("errMsg", errorStr);
		msg.setData(errInfo);
		handler.sendMessage(msg);
	}
}